package com.fee.master.ui;


import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.fee.master.R;
import com.fee.master.model.DayStat;
import com.fee.master.model.MonthStat;
import com.fee.master.util.Constants;
import com.fee.master.util.DateUtil;

public class TodayView extends Activity{
	
	private int todayCallIn;
	
	private int todayCallOut;
	
	private int todayMsgIn;
	
	private int todayMsgOut;
	
	private int monthCallIn;
	
	private int monthCallOut;
	
	private int monthMsgIn;
	
	private int monthMsgOut;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.today);
        
        
        ImageView img1 = (ImageView)this.findViewById(R.id.tab1);
        ImageView img2 = (ImageView)this.findViewById(R.id.tab2);
        ImageView img3 = (ImageView)this.findViewById(R.id.tab3);
        img1.setOnClickListener(header);
        img2.setOnClickListener(header);
        img3.setOnClickListener(header);
        
        String selection = DayStat.DAY+"="+ DateUtil.getCurrentDate("yyyyMMdd");
        Cursor c = this.getContentResolver().query(DayStat.URI_DAY_STAT, null, selection, null, null);
        if(c.moveToFirst()){
        	while(!c.isAfterLast()){
        		String key = c.getString(c.getColumnIndex(DayStat.KEY));
        		int num = c.getInt(c.getColumnIndex(DayStat.NUM));
        		if(Constants.CALL_IN.equals(key)){
        			todayCallIn = num;
        		}else if(Constants.CALL_OUT.equals(key)){
        			todayCallOut = num;
        		}else if(Constants.MSG_IN.equals(key)){
        			todayMsgIn = num;
        		}else if(Constants.MSG_OUT.equals(key)){
        			todayMsgOut = num;
        		}        		
        		c.moveToNext();       		
        	}
        }
        this.startManagingCursor(c);
        
        selection = MonthStat.MONTH+"="+ DateUtil.getCurrentDate("yyyyMM");
        Cursor cursor = this.getContentResolver().query(MonthStat.URI_MONTH_STAT, null, selection, null, null);
        if(cursor.moveToFirst()){
        	while(!cursor.isAfterLast()){
        		String key = cursor.getString(cursor.getColumnIndex(MonthStat.KEY));
        		int num = cursor.getInt(cursor.getColumnIndex(MonthStat.NUM));
        		if(Constants.CALL_IN.equals(key)){
        			monthCallIn = num;
        		}else if(Constants.CALL_OUT.equals(key)){
        			monthCallOut = num;
        		}else if(Constants.MSG_IN.equals(key)){
        			monthMsgIn = num;
        		}else if(Constants.MSG_OUT.equals(key)){
        			monthMsgOut = num;
        		}        		
        		cursor.moveToNext();       		
        	}
        }
        this.startManagingCursor(cursor);
        
        Log.d("todayStat", todayCallIn+""+monthCallIn+""+monthCallOut);
        
        TextView txtTcallout = (TextView)this.findViewById(R.id.today_call_out);
        TextView txtTcallin = (TextView)this.findViewById(R.id.today_call_in);
        TextView txtTmsgout = (TextView)this.findViewById(R.id.today_msg_out);
        TextView txtTmsgin = (TextView)this.findViewById(R.id.today_msg_in);        
        TextView txtMcallout = (TextView)this.findViewById(R.id.month_call_out);
        TextView txtMcallin = (TextView)this.findViewById(R.id.month_call_in);
        TextView txtMmsgout = (TextView)this.findViewById(R.id.month_msg_out);
        TextView txtMmsgin = (TextView)this.findViewById(R.id.month_msg_in);
        
        txtTcallout.setText("打电话："+todayCallOut/60+"分钟");
        txtMcallout.setText("打电话："+monthCallOut/60+"分钟");
        txtTcallin.setText("接电话："+todayCallIn/60+"分钟");        
        txtMcallin.setText("接电话："+monthCallIn/60+"分钟");
        
        txtTmsgout.setText("发短信（条）："+todayMsgOut);
        txtMmsgout.setText("发短信（条）："+monthMsgOut);
        txtTmsgin.setText("收短信（条）："+todayMsgIn);
        txtMmsgin.setText("收短信（条）："+monthMsgIn);
	}
	
	private OnClickListener header = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch(v.getId()){			
			case R.id.tab2:
				Intent i = new Intent(TodayView.this,StatView.class);
				TodayView.this.startActivity(i);
				break;
			case R.id.tab3:
				i = new Intent(TodayView.this,ConfigView.class);
				TodayView.this.startActivity(i);
				break;			
			}			
		}
	};
	
	 public void onStop(){
	    	this.finish();
	    	super.onDestroy();
	 }

}
